﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Conexion;
using System.Net.Sockets;
using System.IO;

namespace Manager
{
    class RequestAgent
    {
        private Object mutex = null;
        private const string CMD_RAM = "01";
        private const string CMD_CPU = "02";
        private const string CMD_DISCO = "03";
        private const string CMD_SERV = "04";
        private ServerNode serv;


        public RequestAgent(Object mutex, ServerNode s)
        {
            this.mutex = mutex;
            this.serv = s;
        }

        public void EnviarRequestAgente(object objServidorComando)
        {
            ServerNode monitor = null;
            try
            {
                lock (mutex)
                {
                    Command srvCmd = (Command)objServidorComando;

                    monitor = srvCmd.Servidor;
                    string cmd = srvCmd.Comando;

                    TcpClient cliente = ClientTCP.obtenerTcpClient(monitor.Ip, monitor.Puerto);
                    string request = CrearComando(cmd);
                    monitor.Vivo = true;

                    //LOG "Sending data"
                    StreamWriter str = new StreamWriter(cliente.GetStream());
                    str.Write(request);
                    str.Flush();

                    //LOG "Comienzo a escuchar..."   
                    string hdr = "";
                    NetworkStream ns;
                    byte[] datos = new byte[1024];

                    ns = cliente.GetStream();
                    ns.Read(datos, 0, 3);
                    hdr = Encoding.UTF8.GetString(datos).Substring(0, 3);

                    if (hdr.Equals("RES"))
                    {
                        ns.Read(datos, 0, 2); 
                        string cmdRecibido = Encoding.UTF8.GetString(datos).Substring(0, 2);
                        if (cmdRecibido.Equals(cmd))
                        {
                            switch (cmd)
                            {
                                case CMD_RAM:
                                    {
                                        ns.Read(datos, 0, 15);
                                        monitor.RamTotal = Encoding.UTF8.GetString(datos).Substring(0, 15);
                                        ns.Read(datos, 0, 15);
                                        monitor.RamLibre = Encoding.UTF8.GetString(datos).Substring(0, 15);
                                        break;
                                    }
                                case CMD_DISCO:
                                    {
                                        ns.Read(datos, 0, 15);
                                        monitor.DiscoTotal = Encoding.UTF8.GetString(datos).Substring(0, 15);
                                        ns.Read(datos, 0, 15);
                                        monitor.DiscoLibre = Encoding.UTF8.GetString(datos).Substring(0, 15);
                                        break;
                                    }
                                case CMD_CPU:
                                    {
                                        ns.Read(datos, 0, 15);
                                        monitor.UsoCpu = Encoding.UTF8.GetString(datos).Substring(0, 15);
                                        break;
                                    }
                                default:
                                    Console.WriteLine("Default case");
                                    break;
                            }
                        }
                        ns.Flush();
                        Console.WriteLine("Respuesta de " + monitor.Nombre + " (" + monitor.Ip + ":" + monitor.Puerto + ") recibida...");
                    }
                    else
                    {
                        Console.WriteLine("Error: leyendo la respuesta del servidor");
                    }

                }
            }
            catch (SocketException)
            {
                Console.WriteLine("El servidor " + monitor.Nombre + " (" + monitor.Ip + ":" + monitor.Puerto + ") no responde");
                monitor.Vivo = false;
            }
            catch (Exception)
            {
                Console.WriteLine("El servidor " + monitor.Nombre + " (" + monitor.Ip + ":" + monitor.Puerto + ") no responde");
                monitor.Vivo = false;
            }

        }

        private string CrearComando(string cmd)
        {
            string comando = "REQ";

            switch (cmd)
            {
                case CMD_CPU:
                    comando += CMD_CPU;
                    break;

                case CMD_DISCO:
                    comando += CMD_DISCO;
                    break;

                case CMD_RAM:
                    comando += CMD_RAM;
                    break;

                default:
                    break;
            }

            return comando;
        }
    }
}
